#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include "nuclei_sdk_soc.h"
#include "nuclei_libopt.h"
#include "nuclei_sdk_hal.h"
#include "nuclei_gpio.h"
#include "anl_printf.h"
unsigned char BMP1[] =
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,
0x80,0x80,0x80,0x80,0x90,0xF0,0xF0,0xE0,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF0,0xE0,0x60,0x60,0x60,0x60,0xE0,
0xE0,0x00,0x00,0xC0,0xF0,0xF0,0x70,0x40,0xC0,0xE0,0xC0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0xC0,0xE0,0xE0,0x70,0x60,0x40,0x00,
0xC0,0xC0,0x80,0x00,0x00,0xE0,0xF0,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x80,0x80,0xB0,0xF0,0xF0,0x80,0x80,0x00,0x80,0x80,0x80,0xF0,
0xF0,0xF0,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x2F,0x27,
0x23,0xA1,0xE1,0xF9,0x7F,0x3F,0x2F,0x25,0xA1,0xE1,0xE1,0xE5,0x27,0x37,0x37,0x22,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x87,0xCF,0xCC,0xDC,0xFC,0xFC,0x0F,
0x87,0x80,0xC6,0xE3,0x31,0x1B,0x1E,0x1F,0x3F,0x73,0xF0,0xE0,0xC0,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x04,0xC4,0xFF,0xFF,0xFF,0xE4,0xC6,0x84,0x00,
0x5C,0xF9,0xE3,0xC3,0xC0,0xFF,0xFF,0xFF,0xC0,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0xC1,0xC1,0xC1,0xE1,0xFF,0xFF,0x31,0x19,0x00,0x31,0xF9,0x99,0x1F,
0x1F,0x9F,0xF9,0xF9,0x71,0x21,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,
0x86,0xC7,0xC7,0xED,0x7C,0x7C,0x3C,0x3E,0x3F,0x3F,0x73,0xF0,0xF0,0x60,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x60,0xFF,0xFF,0x70,0x3F,0x3F,0x11,
0x11,0x01,0x7F,0xFF,0xC3,0xC3,0xC3,0xC3,0xC3,0xFF,0xFF,0x00,0x01,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x0C,0x07,0x03,0xFF,0xFF,0xFF,0x00,0x01,0x03,0x03,
0x00,0x01,0x01,0x01,0x01,0xFF,0xFF,0xFF,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x03,0x63,0xE1,0xFF,0xFF,0x00,0x80,0xC0,0x60,0x30,0x3B,0x1F,
0x1E,0x3F,0x7B,0x70,0xF0,0x60,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xC0,
0xC0,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xC0,0xC0,0xC0,0xC0,
0x80,0x00,0x00,0x00,0x00,0x00,0xC0,0xC0,0xC0,0xC0,0x00,0x00,0x00,0x00,0xC0,0xC0,
0xC0,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,
0xC0,0xC0,0xE0,0xE0,0xE0,0xE0,0xC0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x80,0xC0,0xC0,0xC0,0xE0,0xE0,0xE0,0xE0,0xC0,0xC0,0xC0,0x80,0x00,0x00,0x00,
0x00,0x00,0x00,0xC0,0xC0,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xC0,0xC0,
0xC0,0xE0,0xE0,0xE0,0xE0,0xC0,0xC0,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0xF0,0xFC,0xFF,0x1F,0x07,0x03,
0x13,0xFF,0xFF,0xFE,0x80,0x00,0x00,0x00,0x00,0x80,0xF8,0xFF,0xFF,0x7F,0x0F,0x1F,
0xFF,0xFC,0xF0,0xC0,0xC0,0xF8,0xFF,0xFF,0x3F,0x03,0x00,0x00,0x00,0xF8,0xFF,0xFF,
0xFF,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xF8,0xFE,0xFF,0xFF,0x07,
0x03,0x01,0x00,0x00,0x00,0x01,0xC3,0xFF,0xFF,0xFF,0xFE,0x00,0x00,0x00,0xF8,0xFE,
0xFF,0xFF,0x0F,0x03,0x01,0x00,0x60,0x60,0xE1,0xE3,0xE3,0xE3,0xE3,0x03,0x00,0x00,
0x00,0x80,0xFC,0xFF,0xFF,0xFF,0x07,0x00,0x00,0x00,0xF8,0xFE,0xFF,0xFF,0x0F,0x03,
0x01,0x00,0x00,0x00,0x01,0x03,0x07,0x03,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x1E,0x1F,0x1F,0x0F,0x03,0x03,0x03,0x03,0x03,
0x03,0x1F,0x1F,0x1F,0x1F,0x00,0x00,0x00,0x10,0x1F,0x1F,0x1F,0x07,0x00,0x00,0x00,
0x00,0x03,0x0F,0x1F,0x1F,0x1F,0x1F,0x07,0x00,0x00,0x00,0x10,0x1F,0x1F,0x1F,0x1F,
0x1E,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x00,0x00,0x00,0x07,0x0F,0x1F,0x1F,0x1E,
0x1C,0x1C,0x1C,0x1C,0x1E,0x1F,0x0F,0x07,0x03,0x01,0x00,0x00,0x00,0x00,0x07,0x0F,
0x0F,0x1F,0x1E,0x1C,0x18,0x18,0x18,0x1C,0x1C,0x1F,0x0F,0x0F,0x00,0x00,0x00,0x00,
0x10,0x1F,0x1F,0x1F,0x1F,0x00,0x00,0x00,0x00,0x00,0x03,0x0F,0x0F,0x1F,0x1E,0x1C,
0x1C,0x1C,0x1C,0x1E,0x1F,0x0F,0x07,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};

/**********************************************
// IIC Write Command
**********************************************/
static void Write_IIC_Command(unsigned char IIC_Command)
{
   uint8_t addr = 0x00;
   i2c_mode_addr_config(I2C,0x3c,I2C_WRITE);   //1
   i2c_send_start_write_cmd(I2C);
   while(i2c_flag_get(I2C,TIP));
   i2c_data_transmit(I2C,addr);
   i2c_data_transmit(I2C,IIC_Command);

}

/**********************************************
// IIC Write Data
**********************************************/
static void Write_IIC_Data(unsigned char IIC_Data)
{
    uint8_t addr = 0x40;
    i2c_mode_addr_config(I2C,0x3c,I2C_WRITE);   //1
    i2c_send_start_write_cmd(I2C);
    while(i2c_flag_get(I2C,TIP));
    i2c_data_transmit(I2C,addr);
    i2c_data_transmit(I2C,IIC_Data);
}

static void oled_start_display()
{
	Write_IIC_Command(0xAE);    //display off
	Write_IIC_Command(0x20);	//Set Memory Addressing Mode
	Write_IIC_Command(0x10);	//00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
	Write_IIC_Command(0xb0);	//Set Page Start Address for Page Addressing Mode,0-7
	Write_IIC_Command(0xc8);	//Set COM Output Scan Direction
	Write_IIC_Command(0x00);    //---set low column address
	Write_IIC_Command(0x10);    //---set high column address
	Write_IIC_Command(0x40);    //--set start line address
	Write_IIC_Command(0x81);    //--set contrast control register
	Write_IIC_Command(0xdf);
	Write_IIC_Command(0xa1);    //--set segment re-map 0 to 127
	Write_IIC_Command(0xa6);    //--set normal display
	Write_IIC_Command(0xa8);    //--set multiplex ratio(1 to 64)
	Write_IIC_Command(0x3F);    //
	Write_IIC_Command(0xa4);    //0xa4,Output follows RAM content;0xa5,Output ignores RAM content
	Write_IIC_Command(0xd3);    //-set display offset
	Write_IIC_Command(0x00);    //-not offset
	Write_IIC_Command(0xd5);    //--set display clock divide ratio/oscillator frequency
	Write_IIC_Command(0xf0);    //--set divide ratio
	Write_IIC_Command(0xd9);    //--set pre-charge period
	Write_IIC_Command(0x22);    //
	Write_IIC_Command(0xda);    //--set com pins hardware configuration
	Write_IIC_Command(0x12);
	Write_IIC_Command(0xdb);    //--set vcomh
	Write_IIC_Command(0x20);    //0x20,0.77xVcc
	Write_IIC_Command(0x8d);    //--set DC-DC enable
	Write_IIC_Command(0x14);    //
	Write_IIC_Command(0xaf);    //--turn on oled panel
}


 /**
  * @brief  OLED_SetPos，设置光标
  * @param  x,光标x位置
  *			y，光标y位置
  * @retval 无
  */
void OLED_SetPos(unsigned char x, unsigned char y) //设置起始点坐标
{
	Write_IIC_Command(0xb0+y);
	Write_IIC_Command(((x&0xf0)>>4)|0x10);
	Write_IIC_Command((x&0x0f)|0x01);
}

 /**
  * @brief  OLED_DrawBMP，显示BMP位图
  * @param  x0,y0 :起始点坐标(x0:0~127, y0:0~7);
  *					x1,y1 : 起点对角线(结束点)的坐标(x1:1~128,y1:1~8)
  * @retval 无
  */
void OLED_DrawBMP(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[])
{
	unsigned int j=0;
	unsigned char x,y;

    if(y1%8==0)
		y = y1/8;
    else
		y = y1/8 + 1;
	for(y=y0;y<y1;y++)
	{
		OLED_SetPos(x0,y);
        for(x=x0;x<x1;x++)
		{
			Write_IIC_Data(BMP[j++]);
		}
	}
}
void oled_fill(unsigned char fill_Data)
{
	unsigned char m,n;
	for(m=0;m<8;m++)
	{
		Write_IIC_Command(0xb0+m);		//page0-page1
		Write_IIC_Command(0x00);		//low column start address
		Write_IIC_Command(0x10);		//high column start address
		for(n=0;n<132;n++)
		{
			Write_IIC_Data(fill_Data);
		}

	}

}

void oled_cls(void) //clear screen
{
	oled_fill(0x00);
}


void oled_on(void)
{
	Write_IIC_Command(0X8D);
	Write_IIC_Command(0X14);
	Write_IIC_Command(0XAF);
}

void oled_off(void)
{
	Write_IIC_Command(0X8D);
	Write_IIC_Command(0X10);
	Write_IIC_Command(0XAE);
}



int main()
{
    // i2c Cfg
    i2c_init(I2C,0x3,0x3c,0x1);   //trise   fltr    n = (div +1 )*5 + trise      F(i2c) = F(core)/n
    i2c_enable(I2C);

    while(i2c_flag_get(I2C,BUSY));

	oled_start_display();

	oled_cls();

	oled_fill(0xff);

	OLED_DrawBMP(0,0,128,8,(unsigned char *)BMP1);//测试BMP位图显示

	oled_off();

	oled_on();

    i2c_close_transmit(I2C);

    while(i2c_flag_get(I2C,TIP));

    return 0 ;
}
